import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:demo/models/counter_model.dart';

void main() {
  group('Simple Scoped Model Widget Tests', () {
    late CounterModel counterModel;

    setUp(() {
      counterModel = CounterModel();
    });

    Widget createTestWidget() {
      return MaterialApp(
        home: ScopedModel<CounterModel>(
          model: counterModel,
          child: Scaffold(
            appBar: AppBar(title: const Text('Test')),
            body: Column(
              children: [
                // 显示计数器值
                ScopedModelDescendant<CounterModel>(
                  builder: (context, child, model) {
                    return Text('Counter: ${model.counter}');
                  },
                ),
                
                // 显示用户名
                ScopedModelDescendant<CounterModel>(
                  builder: (context, child, model) {
                    return Text('User: ${model.username}');
                  },
                ),
                
                // 显示状态
                ScopedModelDescendant<CounterModel>(
                  builder: (context, child, model) {
                    return Text('Status: ${model.counterStatus}');
                  },
                ),
                
                // 增加按钮
                ElevatedButton(
                  onPressed: () => counterModel.increment(),
                  child: const Text('Increment'),
                ),
                
                // 重置按钮
                ElevatedButton(
                  onPressed: () => counterModel.reset(),
                  child: const Text('Reset'),
                ),
                
                // 设置用户名按钮
                ElevatedButton(
                  onPressed: () => counterModel.setUsername('Alice'),
                  child: const Text('Set User'),
                ),
              ],
            ),
          ),
        ),
      );
    }

    testWidgets('应该显示初始状态', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证初始状态
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('User: Guest'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);
    });

    testWidgets('点击Increment按钮应该增加计数器', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 查找并点击Increment按钮
      final incrementButton = find.text('Increment');
      expect(incrementButton, findsOneWidget);

      await tester.tap(incrementButton);
      await tester.pump();

      // 验证计数器增加了
      expect(find.text('Counter: 1'), findsOneWidget);
      expect(find.text('Status: Low'), findsOneWidget);
    });

    testWidgets('点击Reset按钮应该重置计数器', (WidgetTester tester) async {
      // 先增加计数器
      counterModel.addMultiple(5);
      
      await tester.pumpWidget(createTestWidget());

      // 验证初始值
      expect(find.text('Counter: 5'), findsOneWidget);

      // 查找并点击Reset按钮
      final resetButton = find.text('Reset');
      expect(resetButton, findsOneWidget);

      await tester.tap(resetButton);
      await tester.pump();

      // 验证计数器重置了
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);
    });

    testWidgets('点击Set User按钮应该更新用户名', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证初始用户名
      expect(find.text('User: Guest'), findsOneWidget);

      // 查找并点击Set User按钮
      final setUserButton = find.text('Set User');
      expect(setUserButton, findsOneWidget);

      await tester.tap(setUserButton);
      await tester.pump();

      // 验证用户名更新了
      expect(find.text('User: Alice'), findsOneWidget);
    });

    testWidgets('多个ScopedModelDescendant应该同步更新', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证初始状态
      expect(find.text('Counter: 0'), findsOneWidget);
      expect(find.text('Status: Zero'), findsOneWidget);

      // 点击Increment按钮
      final incrementButton = find.text('Increment');
      await tester.tap(incrementButton);
      await tester.pump();

      // 验证所有相关的Widget都更新了
      expect(find.text('Counter: 1'), findsOneWidget);
      expect(find.text('Status: Low'), findsOneWidget);

      // 再次点击
      await tester.tap(incrementButton);
      await tester.pump();

      expect(find.text('Counter: 2'), findsOneWidget);
      expect(find.text('Status: Low'), findsOneWidget);
    });

    testWidgets('状态应该根据计数器值正确更新', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 测试不同的状态
      expect(find.text('Status: Zero'), findsOneWidget);

      // 增加到Low状态
      for (int i = 0; i < 5; i++) {
        await tester.tap(find.text('Increment'));
        await tester.pump();
      }
      expect(find.text('Status: Low'), findsOneWidget);

      // 增加到Medium状态
      for (int i = 0; i < 10; i++) {
        await tester.tap(find.text('Increment'));
        await tester.pump();
      }
      expect(find.text('Status: Medium'), findsOneWidget);
    });
  });
}
